package view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraPreviewView extends SurfaceView implements SurfaceHolder.Callback 
{
	private static final String TAG = CameraPreviewView.class.getSimpleName();
	
	private SurfaceHolder _surfaceHolder;
	
	private Scanner _context;

	public CameraPreviewView(Context context, AttributeSet attrs) 
	{
		super(context, attrs);
		
		_context = (Scanner) context;
		_surfaceHolder = getHolder();
		_surfaceHolder.addCallback(this);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
	{
		Log.d(TAG, String.format("surfaceChanged()"));
		_context.startCamera(_surfaceHolder);
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) 
	{
		Log.d(TAG, String.format("SurfaceCreated()"));
		
		setWillNotDraw(false);
		_surfaceHolder = holder;
		_context.startCamera(_surfaceHolder);
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) 
	{
		Log.d(TAG, "SurfaceDestroyed()");
		if(_surfaceHolder.getSurface() == null)
			return;
		
		//restartPreviewSurface();
	}
	
	@SuppressWarnings("unused")
	private void restartPreviewSurface()
	{
		_context.startCamera(_surfaceHolder);
	}
}
